<template>
	<view class="weChatLogin">
		<template v-if="loginStatus == 'loading'">
			<public-loading></public-loading>
		</template>
		<template v-if="loginStatus == 'select'">
			<view class="yes" @click="$_To('login/login')">
				<view>我有账号，我要关联绑定</view>
				<view style="font-size: 22rpx;">登陆后请在账号设置中绑定</view>
			</view>
			<view class="no" @click="clickNoAccountButton">
				我没有账号，帮我生成新账号
			</view>
		</template>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loginStatus:"loading"
			}
		},
		onLoad() {
			uni.login({
				provider:'weixin',
				success: (res) => {
					this.weixinCode = res.code;
					this.postWeChatLoginOne();
				}
			})
		},
		methods: {
			// 微信登录方法，第一次登录
			postWeChatLoginOne(){
				this.$_Request(
					"POST","/api/wechat-login/mini-program/oauth",
					{
						"code":this.weixinCode,
					},
					(res) => {
						if(res.data.code == 0){
							uni.setStorageSync("account",res.data);
							this.$_To("login/userSelection")
						}else{
							this.loginStatus = "select"
						}
					}
				)
			},
			// 没有账号按钮
			clickNoAccountButton(){
				uni.showLoading();
				uni.login({
					provider:'weixin',
					success: (res) => {
						this.weixinCode = res.code;
						this.$_Request(
							"POST","/api/wechat-login/mini-program/oauth",
							{
								"code":this.weixinCode,
								"autoRegister":true
							},
							(res_) => {
								uni.hideLoading();
								if(res_.data.code == 0){
									uni.setStorageSync("account",res_.data);
									this.$_To("login/userSelection")
								}else{
									uni.showToast({
										title:res_.data.message,
										icon:"none",
										duration:1000
									});
								}
							}
						)
					}
				})
			},
		}
	}
</script>

<style lang="scss">
.yes{
	width: 700rpx;
	background: $LanSe;
	display: flex;
	align-items: center;
	justify-content: center;
	margin: 25rpx 25rpx 0 25rpx;
	font-size: 32rpx;
	line-height: 32rpx;
	color: #FFF;
	border-radius: 10rpx;
	padding: 50rpx 0;
	flex-flow: column;
	view{
		color: #FFF;
	}
}
.no{
	width: 700rpx;
	background: $LvSe;
	display: flex;
	align-items: center;
	justify-content: center;
	margin: 25rpx 25rpx 0 25rpx;
	font-size: 32rpx;
	line-height: 32rpx;
	color: #FFF;
	border-radius: 10rpx;
	padding: 50rpx 0;
}
</style>
